import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
import { getlogin, getpxx ,reqLocation} from "network/api.js";
import router from '@/router'

import { Toast } from 'vant';

Vue.use(Toast);
export default new Vuex.Store({
    state: {
        userInfo: {},
        city:"定位中"
    },
    mutations: {
        chundata(state, data) {
            state.userInfo = data
            
        },
        xzcs(state, data) {
            state.city = data
            
        },
        //该suerinfo属性
        changniche(state, value) {
            for (let key in value) {
                console.log(key)
                console.log(value[key])
                state.userInfo[key]=value[key]
            }
            
        },
        //增加地址
        zzjdz(state,data) {
            
            let { addressList } = state.userInfo
            if (typeof data == 'object') {
                let index = addressList.findIndex(item => item.id == data.id)
                if (index < 0) {
                    state.userInfo.addressList.push(data)
                } else {
                    state.userInfo.addressList.splice(index,1,data)
                }
            } else {
                let index = addressList.findIndex(item => item.id == data)
                state.userInfo.addressList.splice(index,1)
            }
           
            
        },
        zsdata(state, data) {
            console.log(typeof data)
            if (typeof data == "object") {
                console.log(typeof data)
                state.userInfo.likeList.push(data)
                console.log(state.userInfo.likeList)
            } else {
                let { likeList } = state.userInfo
                let index = likeList.findIndex(item => 
                     item.product_id == data
                   
                )
                likeList.splice(index, 1)

            }

        },
    },
        actions: {
            async getgxinxi(context, userInfo) {
                const { data, errcode, errmsg } = await getlogin(userInfo)
                
                if(errcode!=0) return Toast(errmsg)
                const { token } = data
                sessionStorage.setItem("token", token)
                context.commit('chundata', data)
                router.back()
            },
            async getpxxx(context) {
                const { data,errcode } = await getpxx()
                console.log(data, errcode)
                
                context.commit('chundata', data)
            },
            getLoaction(context) {
                window.navigator.geolocation.getCurrentPosition(async postition => {
                  const { latitude, longitude } = postition.coords
                  const res = await reqLocation(latitude, longitude)
                  const {city} = res.result.addressComponent
                  context.commit("xzcs",city)
                })
              }
          
            
    },
        

        modules: {

        }
    })